package leetcode;

import java.util.Arrays;

/**
 * @创建人 MBY381
 * @创建时间 2022/3/30 16:38
 * @描述 912题是数组排序，时间复杂度不能是on'2   此处仅列出快速排序
 */
public class Solution0912 {
    static void sort(int[] nums, int startIndex, int endIndex) {
        if (startIndex >= endIndex) {
            return;
        }
        int mid = nums[(startIndex + endIndex) >> 1];
        int left = startIndex, right = endIndex;
        while (left <= right) {
            while (nums[left] < mid)
                left++;
            while (nums[right] > mid)
                right--;

            if (left <= right) {
                int t = nums[right];
                nums[right] = nums[left];
                nums[left] = t;
                left++;
                right--;
            }
        }
        if (startIndex < right)
            sort(nums, startIndex, right);
        if (left < endIndex)
            sort(nums, left, endIndex);
    }

    public static void main(String[] args) {
        System.out.println("正确答案：    [1, 2, 3, 4, 5, 6, 7, 8, 9, 21, 227, 259, 381, 696, 782]");
        int[] nums = new int[]{0xE3, 381, 8, 6, 4, 7, 9, 3, 5, 2, 1, 782, 696, 259, 0x15};
        sort(nums, 0, nums.length - 1);
        System.out.println(Arrays.toString(nums));
    }

    public int[] sortArray(int[] nums) {
        sort(nums, 0, nums.length - 1);
        return nums;
    }
}
